package by.grsu.pereverzev.page;

import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import by.grsu.pereverzev.app.PereverzevWebApplication;
import by.grsu.pereverzev.dao.impl.ApplicationDao;
import by.grsu.pereverzev.dao.impl.BillDao;
import by.grsu.pereverzev.dao.impl.PassengerDao;
import by.grsu.pereverzev.entity.Application;
import by.grsu.pereverzev.entity.Bill;
import by.grsu.pereverzev.entity.Passenger;

public class ApplicationListPage extends WebPage{
	
	private final ApplicationDao applicationDao = new ApplicationDao(PereverzevWebApplication.DATA_XML_FOLDER);
	private final PassengerDao passengerDao = new PassengerDao(PereverzevWebApplication.DATA_XML_FOLDER);
	private final BillDao billDao = new BillDao(PereverzevWebApplication.DATA_XML_FOLDER);
	
	public ApplicationListPage(final Passenger passenger) {
		super();
		List<Application> apps = applicationDao.getByPassengerId(passenger.getId());
		
		add(new DataView<Application>("table-rows", new ListDataProvider<Application>(apps)) {
			@Override
			protected void populateItem(final Item<Application> item) {
				final Application application = item.getModelObject();
				Bill bill = billDao.get(application.getBillId());
				
				item.add(new Label("id", application.getId()));			
				item.add(new Label("passengerName", passenger.getName()));
				item.add(new Label("cost", bill.getValue()));
				item.add(new Label("isPayed", bill.isPayed()));
				item.add(new Label("flightId", application.getFlightId()));
					

				item.add(new Link("deleteLink") {
					@Override
					public void onClick() {
						applicationDao.delete(application.getId());
						setResponsePage(new ApplicationListPage(passenger));
					}
				});
			}
		});
		
	}

}
